package controller

import (
	"ahutoj/web/io/constanct"
	"ahutoj/web/io/request"
	"ahutoj/web/io/response"
	"ahutoj/web/logic"
	"ahutoj/web/utils"

	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

// @Summary	增加权限
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		AddPermissionReq	body		request.AddPermissionReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/admin/permission/add/ [post]
func AddPermission(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.AddPermissionReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.AddPermission(ctx, req)
	if err != nil {
		logger.Errorf("call AddPermission failed,req=%+v, err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	编辑权限
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		EditPermissionReq	body		request.EditPermissionReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/admin/permission/edit/ [post]
func EditPermission(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.EditPermissionReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.EditPermission(ctx, req)
	if err != nil {
		logger.Errorf("call EditPermission failed,req=%+v, err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	删除权限
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		DeletePermissionReq	body		request.DeletePermissionReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/admin/permission/delete [post]
func DeletePermission(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.DeletePermissionReq)
	if err := ctx.ShouldBindWith(req, binding.JSON); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.DeletePermission(ctx, req)
	if err != nil {
		logger.Errorf("call DeletePermission failed,req=%+v, err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取管理员权限列表
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		PermissionListReq	body		request.PermissionListReq	true	"-"
// @Success	200					{object}	response.PermissionListResp
// @Router		/api/admin/permission/list/ [get]
func GetListPermission(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.PermissionListReq)
	if err := ctx.ShouldBindWith(req, binding.Query); err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.GetPermissionList(ctx, req)
	if err != nil {
		logger.Errorf("call GetPermissionList failed,req=%+v, err=%s", *req, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取用户权限
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		id	path		string	true	"用户ID" "-"
// @Success	200	{object}	response.PermissionResp
// @Router		/api/admin/permission/{id} [get]
func GetPermission(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	UID := ctx.Param("id")

	if UID == "" {
		logger.Errorf("UID为空")
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.GetPermission(ctx, UID)
	if err != nil {
		logger.Errorf("call GetPermission failed,UID=%+v, err=%s", UID, err.Error())
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	范围添加用户
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		AddUsersRangeReq	body		request.AddUsersRangeReq	true	"-"
// @Success	200					{object}	response.AddUsersResp
// @Router		/api/admin/user/Range [post]
func AddUsersRange(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.AddUsersRangeReq)
	err := ctx.ShouldBindWith(req, binding.JSON)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.AddUsersRange(ctx, *req)
	if err != nil {
		logger.Errorf("call AddUsers err=%s, req=%+v", err.Error(), utils.Sdump(req))
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	添加用户
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		AddUsersReq	body		request.AddUsersReq	true	"-"
// @Success	200			{object}	response.AddUsersResp
// @Router		/api/admin/users/ [post]
func AddUsers(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.AddUsersReq)
	err := ctx.ShouldBindWith(req, binding.JSON)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.AddUsers(ctx, req)
	if err != nil {
		logger.Errorf("call AddUsers err=%s, req=%+v", err.Error(), utils.Sdump(req))
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	获取用户列表,可以通过传递token进行用户名/uid的检索
// @Description
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		GetUsersReq	query		request.GetUsersReq	true	"-"
// @Success	200			{object}	response.AddUsersResp
// @Router		/api/admin/users/ [get]
func GetUsers(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.GetUsersReq)
	err := ctx.ShouldBindWith(req, binding.Query)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.GetUsers(ctx, req)
	if err != nil {
		logger.Errorf("call GetUsers err=%s, req=%+v", err.Error(), utils.Sdump(req))
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}

// @Summary	管理员修改用户关键信息
// @Description 根据UID改密码  根据UID改邮箱 根据邮箱改密码 根据邮箱改UID 
// @Tags		Admin
// @Accept		json
// @Produce	json
// @Param		AdminChangeUserReq	body		request.AdminChangeUserReq	true	"-"
// @Success	200					{object}	response.Response
// @Router		/api/admin/user/edit/ [post]
func AdminChangeUser(ctx *gin.Context) {
	logger := utils.GetLogInstance()
	req := new(request.AdminChangeUserReq)
	err := ctx.ShouldBindWith(req, binding.JSON)
	if err != nil {
		logger.Errorf("call ShouldBindWith failed, err = %s", err.Error())
		response.ResponseError(ctx, constanct.InvalidParamCode)
		return
	}
	resp, err := logic.AdminChangeUser(ctx, req)
	if err != nil {
		logger.Errorf("call AdminChangeUser err=%s, req=%+v", err.Error(), utils.Sdump(req))
		response.ResponseError(ctx, constanct.ServerErrorCode)
		return
	}
	response.ResponseOK(ctx, resp)
}
